//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Controls;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class TableControl: Control
    {


        public int Top
        {
            get { return (int)GetValue(TopProperty); }
            set { SetValue(TopProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Top.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TopProperty =
            DependencyProperty.Register("Top", typeof(int), typeof(TableControl), new UIPropertyMetadata(0));


        public int Left
        {
            get { return (int)GetValue(LeftProperty); }
            set { SetValue(LeftProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Left.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LeftProperty =
            DependencyProperty.Register("Left", typeof(int), typeof(TableControl), new UIPropertyMetadata(0));


        public Table Table
        {
            get { return (Table)GetValue(TableProperty); }
            set { SetValue(TableProperty, value); }
        }


        public bool ShowColumnStripes
        {
            get { return (bool)GetValue(ShowColumnStripesProperty); }
            set { SetValue(ShowColumnStripesProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowColumnStripes.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowColumnStripesProperty =
            DependencyProperty.Register("ShowColumnStripes", typeof(bool), typeof(TableControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));



        public bool ShowFirstColumn
        {
            get { return (bool)GetValue(ShowFirstColumnProperty); }
            set { SetValue(ShowFirstColumnProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowFirstColumn.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowFirstColumnProperty =
            DependencyProperty.Register("ShowFirstColumn", typeof(bool), typeof(TableControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));



        public bool ShowLastColumn
        {
            get { return (bool)GetValue(ShowLastColumnProperty); }
            set { SetValue(ShowLastColumnProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowLastColumn.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowLastColumnProperty =
            DependencyProperty.Register("ShowLastColumn", typeof(bool), typeof(TableControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));



        public bool ShowRowStripes
        {
            get { return (bool)GetValue(ShowRowStripesProperty); }
            set { SetValue(ShowRowStripesProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowRowStripes.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowRowStripesProperty =
            DependencyProperty.Register("ShowRowStripes", typeof(bool), typeof(TableControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));


        // Using a DependencyProperty as the backing store for Table.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TableProperty =
            DependencyProperty.Register("Table", typeof(Table), typeof(TableControl), new UIPropertyMetadata(null, OnTableChanged));

        private static void OnTableChanged(DependencyObject owner, DependencyPropertyChangedEventArgs args)
        {
            TableControl This = (TableControl)owner;
            var old = (Table)args.OldValue;
            if (old != null)
                old.PropertyChanged -= This.TablePropertyChanged;
            var newTable = (Table)args.NewValue;
            if (newTable != null)
                newTable.PropertyChanged += This.TablePropertyChanged;


        }

        void TablePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            InvalidateVisual();
        }

        protected Rect GetRect(int row1, int col1, int row2, int col2, Worksheet Worksheet)
        {
            return new Rect(new Point(GetX(Worksheet, col1),
                                      GetY(Worksheet, row1)),
                            new Point(Worksheet.Columns[col2].Offset - Worksheet.Columns[Left].Offset,
                                      Worksheet.Rows[row2].Offset - Worksheet.Rows[Top].Offset));
        }

        protected double GetY(Worksheet Worksheet, int row1)
        {
            return Worksheet.Rows[row1].Offset - Worksheet.Rows[Top].Offset;
        }

        protected double GetX(Worksheet Worksheet, int col1)
        {
            return Worksheet.Columns.GetOffset(col1) - Worksheet.Columns[Left].Offset;
        }
    }
}